package edu.imac.angryfood.states;


import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import edu.imac.angryfood.game.Angryfood;

/** state of the main menu */

public class MainMenuState extends BasicGameState implements ComponentListener{
	
	public static int ID;
	public Angryfood parent;
	private StateBasedGame game;
	
	/** The areas defined */
	private MouseOverArea[] areas;
	/** The logo of the game */
	private Image logo;
	/** The background image */
	private Image background;
	/** The button image */
	private Image cadre; 
	/** Spacing between the buttons */
	private int spacing = 220;	

	/**
     * Constructor of the state.
     * 
     * 
     */

	public MainMenuState(Angryfood game, int ID) {
		super();
		MainMenuState.ID = ID;
		this.parent = game;

	}
	

	
	/** initializes the game
	 * @param container contains the game
	 * @param game the game
	 * @throws SlickException
	 */

	
	public void init(GameContainer container, StateBasedGame game) throws SlickException {
		this.game = game;	
		

		/** Initialization of the images */
		logo =  new Image("res/img/menu/logoAF.png");
		background = new Image("res/img/menu/fondAF.jpg");
		cadre = new Image("res/img/menu/cadre.png"); 


		/** Areas that can be clicked */
		areas = new MouseOverArea[2];		
			
		for (int i=0;i<2;i++) {	
			areas[i] = new MouseOverArea(container, cadre, 180 + i *spacing, 500 , 200, 50, this);
			areas[i].setNormalColor(new Color(1,1,1,0.8f));
			areas[i].setMouseOverColor(new Color(1,1,1,1f));
		}
	}

	/** render the graphics in slick
	 * @param container contains the game
	 * @param game the game
	 * @param g the graphics
	 */


	public void render(GameContainer container, StateBasedGame game, Graphics g) {
		
		background.draw(0, 0, 800, 600);
		logo.draw(400,0,400,100);
	
		/** Draw of the images*/
		areas[0].render(container, g);
		g.drawString("Jouer", 250  , 515 ); // titre de la vignette
		areas[1].render(container, g);
		g.drawString("Credits", 250 + spacing  , 515 ); // titre de la vignette
		
	}

	/** update the physic world
	 * @param container the game container
	 * @param game the game
	 * @param delta
	 */	
	public void update(GameContainer container, StateBasedGame game, int delta) {
	}

	
	/**
     * Function used to click on the areas
     * 
     * @param source Area of the screen which is clicked
     */

	@Override
	public void componentActivated(AbstractComponent source) {	
			if (source == areas[0]) 
				game.enterState(1, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));	
			if (source == areas[1]) 
				game.enterState(2, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
		
	}
		
	
	@Override
	public int getID() {
		return MainMenuState.ID;
	}


}
